package com.jamal2362.apktool.fragment.files;

import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import java.io.File;
import com.jamal2362.apktool.R;
import com.jamal2362.apktool.fragment.FilesFragment;
import com.jamal2362.apktool.util.IconFactory;
import com.jamal2362.apktool.util.IconTypes;
import com.jamal2362.apktool.util.Settings;

public class BackItem implements Item {
	private final File currentDir;
	private boolean preventClick;

	public BackItem(File curDir) {
		this.currentDir = curDir;
		this.preventClick = false;
	}

	@Override
	public void setup(View root, ImageView icon, TextView name, TextView details, final Refreshable refresh) {
		root.setBackgroundColor(Color.TRANSPARENT);
		IconFactory icFactory = new IconFactory(name.getContext());
		int folderColor = (Settings.lightTheme ? IconTypes.colorFolderLight : IconTypes.colorFolderDark);
		icon.setImageBitmap(icFactory.makeIcon(folderColor, R.drawable.ic_folder));
		String backText = " ..";
		if (refresh.isSelectionMode())
			backText = name.getContext().getApplicationContext().getString(R.string.mselect_all);
		name.setText(backText);
		details.setVisibility(View.GONE);
		icon.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View p1) {
					click(p1, refresh);
				}
			});
		icon.setOnLongClickListener(new OnLongClickListener() {
				@Override
				public boolean onLongClick(View p1) {
					refresh.selectAll(false);
					preventClick = true;
					return true;
				}
			});
	}

	@Override
	public boolean click(View view, final Refreshable refresh) {
		refresh.setScrollPosition(currentDir.getAbsolutePath());
		if (preventClick) {
			preventClick = false;
			return true;
		}
		if (refresh.isSelectionMode()) {
			refresh.selectAll(false);
		} else {
			refresh.refresh(currentDir.getParentFile());
		}
		return true;
	}

	@Override
	public boolean edit(FilesFragment frag) {
		return false;
	}


	@Override
	public boolean longClick(View view, Refreshable refresh) {
		// TODO: Implement this method
		return false;
	}

	@Override
	public void process(FilesAdapter adapter) {
	}

	@Override
	public int getProperty() {
		return PROPERTY_BACK;
	}

	@Override
	public int compareTo(Item p1) {
		return PROPERTY_BACK - p1.getProperty();
	}
}
